import axios from 'axios'
import router from '../router/index.js'
import qs from 'qs'
axios.defaults.baseURL = process.env.VUE_APP_BASE_API
console.log(axios.defaults.baseURL)
axios.defaults.headers.post['Content-Type'] =
  'application/x-www-form-urlencoded'

// http request 拦截器

axios.defaults.timeout = 10000
axios.defaults.retry = 4
axios.defaults.retryDelay = 1000

axios.interceptors.request.use(
  config => {
    // if (config.method != "get") {
    //   config.data = qs.stringify(config.data);
    // }
    if (localStorage.getItem('token')) {
      config.headers.Authorization = localStorage.getItem('token')
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)
//http response 拦截器
axios.interceptors.response.use(
  response => {
    // 接口判断未登录
    return response
  },
  error => {
    console.log(error.response.status, 'error')
    if (error.response.status === 401 && router.currentRoute.name !== 'Login') {
      localStorage.removeItem('token')
      router.replace({
        path: '/Login',
        query: { redirect: router.currentRoute.fullPath }
      })
    }
    return Promise.reject(error.response.data) // 返回接口返回的错误信息
  }
)

export default axios

// 定义请求
//export const getOrderList = params => { return axios.get('/orderList', params).then(res => res.data) }

// export const menuList = params => {
//   return axios.get('/api/v1/sysMenu/menuListByParentId/'+`${params}`).then(res => res.data);
// };
export const login = params => {
  return axios.post('/login', qs.stringify(params)).then(res => res.data)
}
export const checkcode = params => {
  return axios.post('/v1/open/captcha', params).then(res => res.data)
}
export const getUserInfo = params => {
  return axios.post('/v1/sysUser/getUserInfo', params).then(res => res.data)
}
